﻿using System;
using System.Collections.Generic;
using System.Text;
using ASBCC.Database.Entity;
using ASBCC.Database.Service;
using SourceCode.Workflow.Client;

namespace ASBCC.Workflow.K2Manager
{
    public interface IActionHandler
    {
        void Execute();
        int ProcInstID { get; }
    }

    public interface IEmailAction
    {
        void SendMail(int formId, string templateKey);
    }

    public class ActionHandlerBase : IActionHandler
    {
        #region IActionHandler Members

        public void Execute()
        {
            ClearInternalMembers();
            StoreK2Fields();


            ExcuteAction();
            BuildActionRecord();
            BuildMyActivity();
            BuildProcessLog();
            BuildProcessStamp();
            BuildLeadingTime();
            BuildProcessComment();
            SendMail();
            //SendMail2();
        }


        public int ProcInstID
        {
            get
            {
                return _procInstID;
            }
        }

        #endregion

        #region Constructor

        public ActionHandlerBase(SerializableDictionary dataFields, K2AppForm appForm, K2WorkItem workItem, K2ActionInfo actionInfo, string comment, K2AuditDestination auditDestination, List<K2User> outputDestinationUsers)
        {
            _dataFields = dataFields;
            _appForm = appForm;
            _workItem = workItem;
            _actionInfo = actionInfo;
            _comment = comment;
            _auditDestination = auditDestination;

            if (workItem != null)
            {
                _procInstID = workItem.ProcInstId;
            }

            _flowProcess = K2DataHelper.GetProcess(WorkItem.ProcName);
            _outputDestinationUsers = outputDestinationUsers;
        }

        #endregion

        #region Helper Members
        private bool _IsExcuteAction = true;
        public bool IsExcuteAction
        {
            get
            {
                return _IsExcuteAction;
            }
        }
        private bool ParseIsExcuteAction()
        {
            //TODO:判断是否执行流程 并发且该流程的待办数量为1时
            Flow_ProcessConfigTableEntity processConfig = GetCurrentProcessConfig();
            if (processConfig != null && !string.IsNullOrEmpty(processConfig.ApproveMode) && (ApproveMode)Enum.Parse(typeof(ApproveMode), processConfig.ApproveMode) == ApproveMode.All)
            {
                MyActivityTableEntitySet set = MyActivityManager.SelectByCondition(string.Format(" K2ProcInstID={0}", WorkItem.ProcInstId));
                if (set.Count > 1)
                {
                    if (_myActivityListToBeMoved == null)
                        _myActivityListToBeMoved = new List<MyActivityTableEntity>();
                    foreach (MyActivityTableEntity activity in set)
                    {
                        if (activity.OwnerID == _appForm.CurrentUser.UserId)
                        {
                            _myActivityListToBeMoved.Add(activity);
                            break;
                        }
                    }
                    return false;
                }
            }
            return true;

        }
        protected int _procInstID;

        /// <summary>
        /// 判断下一步是不是流向自身节点
        /// </summary>
        /// <returns></returns>
        private bool GotoSelf()
        {
            return AuditDestination != null && AuditDestination.ActivityDestinationList.Count == 1 && AuditDestination.ActivityDestinationList[0].ActivityName == WorkItem.ActivityName;
        }

        //protected void RedirectWorklistItem()
        //{
        //    string fromUserName = WorkItem.Destination;
        //    string toUserName = K2Helper.GetAdminDestinationName();
        //    int procInstID = WorkItem.ProcInstId;
        //    int actInstDestID = WorkItem.ActInstDestId;
        //    K2Admin.RedirectWorklistItem(fromUserName, toUserName, procInstID, actInstDestID, 0);
        //}

        protected string SerialNumber
        {
            get
            {
                if (WorkItem == null) return string.Empty;
                return K2Helper.GetSerialNumber(WorkItem.ProcInstId, WorkItem.ActInstDestId);
            }
        }

        protected string ProcessName
        {
            get { return K2Helper.GetProcessName(WorkItem.ProcName); }
        }

        protected SourceCode.Workflow.Management.WorklistItems _allWorklistItemsAfterAction;
        protected SourceCode.Workflow.Management.WorklistItems AllWorklistItemsAfterAction
        {
            get { return _allWorklistItemsAfterAction; }
            set { _allWorklistItemsAfterAction = value; }
        }

        protected MyActivityTableEntitySet _myActivitySetBeforeMoving;
        protected MyActivityTableEntitySet MyActivitySetBeforeMoving
        {
            get { return _myActivitySetBeforeMoving; }
            set { _myActivitySetBeforeMoving = value; }
        }

        protected List<MyActivityTableEntity> _myActivityListToBeMoved;
        public List<MyActivityTableEntity> MyActivityListToBeMoved
        {
            get { return _myActivityListToBeMoved; }
            set { _myActivityListToBeMoved = value; }
        }

        private List<SourceCode.Workflow.Management.WorklistItem> _newWorkitemListAfterAction;
        protected List<SourceCode.Workflow.Management.WorklistItem> NewWorkitemListAfterAction
        {
            get { return _newWorkitemListAfterAction; }
            set { _newWorkitemListAfterAction = value; }
        }

        private List<MyActivityTableEntity> _newMyActivityList;
        protected List<MyActivityTableEntity> NewMyActivityList
        {
            get { return _newMyActivityList; }
            set { _newMyActivityList = value; }
        }

        private Guid _actionRecordID;
        protected Guid ActionRecordID
        {
            get { return _actionRecordID; }
            set { _actionRecordID = value; }
        }

        private void ClearInternalMembers()
        {
            _allWorklistItemsAfterAction = null;
            _myActivitySetBeforeMoving = null;
            _myActivityListToBeMoved = null;
            _newWorkitemListAfterAction = null;
            _newMyActivityList = null;
        }

        protected void MergeWorklistItemsAndMyActivitySet()
        {
            K2Helper.MergeWorklistItemsAndMyActivitySet(_allWorklistItemsAfterAction, _myActivitySetBeforeMoving, out _myActivityListToBeMoved, out _newWorkitemListAfterAction);
        }

        protected void MoveMyActivityList()
        {
            K2DataHelper.MoveMyActivityList(_myActivityListToBeMoved);
        }

        protected void CreateNewMyActivityList()
        {
            _newMyActivityList = new List<MyActivityTableEntity>();
            foreach (SourceCode.Workflow.Management.WorklistItem worklistItem in _newWorkitemListAfterAction)
            {
                MyActivityTableEntity entity = LoadMyActivity(worklistItem);
                _newMyActivityList.Add(entity);
                if (OutputDestinationUsers != null)
                {
                    OutputDestinationUsers.Add(new K2User(entity.OwnerID ?? 0, entity.OwnerEn, entity.OwnerCn));
                }
                int delegateTo = 0;
                if (entity.OwnerID.HasValue)
                {
                    delegateTo = GetAccountOfDelegateTo(entity.OwnerID.Value);
                }
                if (delegateTo > 0 && delegateTo != entity.OwnerID.Value)
                {
                    MyActivityTableEntity copy = K2Helper.CloneMyActivity(entity);
                    copy.MyActivityID = Guid.NewGuid();
                    copy.DelegateFromID = entity.OwnerID;
                    copy.DelegateFromEn = entity.OwnerEn;
                    copy.DelegateFromCn = entity.OwnerCn;

                    K2User delegateToUser = K2DataHelper.GetK2UserById(delegateTo);
                    if (delegateToUser != null)
                    {
                        copy.OwnerID = delegateToUser.UserId;
                        copy.OwnerEn = delegateToUser.UserEn;
                        copy.OwnerCn = delegateToUser.UserCn;
                    }
                    _newMyActivityList.Add(copy);
                    if (OutputDestinationUsers != null)
                    {
                        OutputDestinationUsers.Add(new K2User(copy.OwnerID ?? 0, copy.OwnerEn, copy.OwnerCn));
                    }
                }
            }
            K2DataHelper.CreateNewMyActivityList(_newMyActivityList);
        }

        protected bool CheckMailApproverRule(EmployeeTableEntity entity)
        {
            return entity != null && !string.IsNullOrEmpty(entity.Email);// && (!entity.ApproverRule.HasValue || entity.ApproverRule.Value == (int)MailRule.PerActivity);
        }

        protected bool CheckMailApplicantRule(EmployeeTableEntity entity)
        {
            return entity != null && !string.IsNullOrEmpty(entity.Email);// && (!entity.ApplicantRule.HasValue || entity.ApplicantRule.Value == (int)MailRule.PerActivity);
        }

        private Flow_ProcessConfigTableEntity GetCurrentProcessConfig()
        {
            return GetProcessConfig(WorkItem.ProcName, WorkItem.ActivityName);
        }

        private Flow_ProcessConfigTableEntity GetNewNodeProcessConfig(string newActivityName)
        {
            return GetProcessConfig(WorkItem.ProcName, newActivityName);
        }

        private Flow_ProcessConfigTableEntity GetProcessConfig(string procName, string activityName)
        {
            return K2DataHelper.GetProcessConfig(procName, activityName);
        }

        protected string GetActivityDisplayName(string newActivityName)
        {
            return GetNewNodeProcessConfig(newActivityName).ActivityDisplayName;
        }

        protected DateTime GetDutyDate(string newActivityName)
        {
            int days = 0;
            int.TryParse(GetNewNodeProcessConfig(newActivityName).DutyDays.ToString(), out days);
            return DateTime.Now.Add(TimeSpan.FromDays(days));
        }

        protected bool? GetEnableBatchAction(string newActivityName)
        {
            return GetNewNodeProcessConfig(newActivityName).EnableBatchAction;
        }

        /// <summary>
        /// 获得所有下级审批人,并将其用分号串联
        /// </summary>
        /// <returns></returns>
        private string GetNextAuditorsConnect()
        {
            string auditors = string.Empty;

            //当走向最后一个节点时,AuditDestination为空
            if (AuditDestination == null)
                return string.Empty;

            //其它动作下级审批人通过AuditDestination传入取值
            foreach (K2AuditDestination.ActivityDestination auditDestination in AuditDestination.ActivityDestinationList)
            {
                foreach (int user in auditDestination.UserArr)
                {
                    auditors = auditors + ";" + user.ToString();
                }
            }
            return auditors.TrimStart(';');
        }

        /// <summary>
        /// 获得代理人帐号
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        protected int GetAccountOfDelegateTo(int userId)
        {
            Flow_ProcessTableEntity processInfo = _flowProcess;
            if (string.IsNullOrEmpty(processInfo.Category))
                return -1;
            UserDelegationTableEntity userDelegation = K2DataHelper.GetUserDelegationInfo(processInfo.Category, userId);
            if (userDelegation == null)
                return -1;
            return userDelegation.DelegateTo.Value;
        }

        private MyActivityTableEntity LoadMyActivity(SourceCode.Workflow.Management.WorklistItem worklistItem)
        {
            MyActivityTableEntity myActivity = new MyActivityTableEntity();
            myActivity.ApplicantID = AppForm.Applicant.UserId;
            myActivity.ApplicantEn = AppForm.Applicant.UserEn;
            myActivity.ApplicantCn = AppForm.Applicant.UserCn;
            myActivity.ApplicationNo = AppForm.ApplicationNo;
            myActivity.ArrvialDate = DateTime.Now;
            myActivity.CreatedDate = DateTime.Now;
            myActivity.CreatorID = AppForm.Creator.UserId;
            myActivity.CreatorEn = AppForm.Creator.UserEn;
            myActivity.CreatorCn = AppForm.Creator.UserCn;
            string strOwnerId = K2Helper.K2ActionerToOwner(worklistItem.Actioner.Name);
            int ownerId;
            bool parseOwnerId = int.TryParse(strOwnerId, out ownerId);
            if (parseOwnerId)
            {
                K2User owner = K2DataHelper.GetK2UserById(ownerId);
                myActivity.OwnerID = owner.UserId;
                myActivity.OwnerEn = owner.UserEn;
                myActivity.OwnerCn = owner.UserCn;
            }

            //myActivity.DelegateFrom;
            myActivity.DutyDate = GetDutyDate(worklistItem.ActivityName);
            myActivity.EnableBatchAction = GetEnableBatchAction(worklistItem.ActivityName);
            myActivity.FormID = AppForm.FormId;
            myActivity.FormStatus = string.Empty;
            myActivity.FormURL = GetFormURL();
            myActivity.K2ActInstDestID = worklistItem.ActInstDestID;
            myActivity.K2ActivityName = worklistItem.ActivityName;
            myActivity.K2Destination = worklistItem.Destination;
            myActivity.K2Folio = worklistItem.Folio;
            myActivity.K2ProcInstID = worklistItem.ProcInstID;
            myActivity.K2SerianNumber = K2Helper.GetSerialNumber(worklistItem.ProcInstID, worklistItem.ActInstDestID);
            myActivity.ModifiedDate = DateTime.Now;
            myActivity.MyActivityID = Guid.NewGuid();
            myActivity.ProcName = WorkItem.ProcName;
            myActivity.SourceActionType = ActionInfo.ActionType;
            myActivity.Status = (int)MyActivityStatus.Normal;
            myActivity.Title = AppForm.Title;
            myActivity.ActionRecordID = ActionRecordID;
            return myActivity;
        }

        private void GetDataAndXmlFields(out string strDataFields, out string strXmlFields)
        {
            strDataFields = string.Empty;
            strXmlFields = string.Empty;
            if (WorkItem.ProcInstId <= 0)
            {
                if (DataFields != null)
                {
                    strDataFields = SerializableDictionaryHelper.DictionarySerializer(DataFields, typeof(SerializableDictionary));
                }
                if (AuditDestination != null)
                {
                    strXmlFields = AuditDestination.ExportText();
                }
            }
            else
            {
                SerializableDictionary dataFieldDict;
                K2AuditDestination auditDestination;
                Connection conn = K2Client.OpenConnection();
                try
                {
                    K2Client.GetDataAndXmlFields(conn, WorkItem.ProcInstId, out dataFieldDict, out auditDestination);
                }
                finally
                {
                    conn.Close();
                }

                SerializableDictionary mergedDataField = K2Helper.MergeDictionary(dataFieldDict, DataFields);
                strDataFields = SerializableDictionaryHelper.DictionarySerializer(mergedDataField, typeof(SerializableDictionary));

                if (AuditDestination != null)
                {
                    strXmlFields = AuditDestination.ExportText();
                }
            }
        }

        public string GetFormURL()
        {
            string formURL = (string)this._dataFields[K2Consts.DataField_Key_FormURL];
            if (string.IsNullOrEmpty(formURL)) throw new Exception("Failed to invoke GetFormURL().");
            return formURL;
        }

        public void GotoActivity(int procInstId)
        {
            Connection connection = K2Client.OpenConnection();
            try
            {
                K2Client.UpdateDataAndXmlFields(connection, procInstId, AuditDestination, DataFields);
            }
            finally
            {
                connection.Close();
            }

            K2Admin.GotoActivity(procInstId, AuditDestination.ActivityDestinationList[0].ActivityName);
        }

        public void GotoEndActivity(int procInstId)
        {
            K2Admin.GotoActivity(procInstId, "End");
        }

        public void StoreK2Fields()
        {
            string strDataFields, strXmlFields;
            GetDataAndXmlFields(out strDataFields, out strXmlFields);
            _storedDataFields = strDataFields;
            _storedXmlFields = strXmlFields;
        }

        protected bool ValidateAuditDestination(K2AuditDestination auditDestination)
        {
            if (auditDestination.ActivityDestinationList.Count == 0) return false;
            if (auditDestination.ActivityDestinationList[0].ActivityName != K2ActivityName.End)
            {
                if (auditDestination.ActivityDestinationList[0].UserArr == null ||
                    auditDestination.ActivityDestinationList[0].UserArr.Length == 0 ||
                    auditDestination.ActivityDestinationList[0].UserArr[0] <= 0)
                {
                    return false;
                }
            }
            return true;
        }

        #endregion

        #region Properties
        //全部该为可写入,满足初次提交时,重新生成

        private SerializableDictionary _dataFields;
        public SerializableDictionary DataFields { get { return _dataFields; } }

        private K2AppForm _appForm;
        protected K2AppForm AppForm { get { return _appForm; } set { _appForm = value; } }

        private K2WorkItem _workItem;
        protected K2WorkItem WorkItem { get { return _workItem; } set { _workItem = value; } }

        private K2ActionInfo _actionInfo;
        protected K2ActionInfo ActionInfo { get { return _actionInfo; } set { _actionInfo = value; } }

        private string _comment;
        protected string Comment { get { return _comment; } set { _comment = value; } }

        private K2AuditDestination _auditDestination;
        protected K2AuditDestination AuditDestination { get { return _auditDestination; } set { _auditDestination = value; } }

        private string _storedDataFields;
        protected string StoredDataFields { get { return _storedDataFields; } set { _storedDataFields = value; } }

        private string _storedXmlFields;
        protected string StoredXmlFields { get { return _storedXmlFields; } set { _storedXmlFields = value; } }

        private Flow_ProcessTableEntity _flowProcess;
        protected Flow_ProcessTableEntity FlowProcess { get { return _flowProcess; } set { _flowProcess = value; } }

        private List<K2User> _outputDestinationUsers;
        public List<K2User> OutputDestinationUsers { get { return _outputDestinationUsers; } set { _outputDestinationUsers = value; } }

        #endregion

        #region Virtual Methodes

        /// <summary>
        /// 执行流程动作
        /// 对于正常ActionType比如Approve、Review，使用基类的函数，否则需要重载
        /// </summary>
        protected virtual void ExcuteAction()
        {
            Connection connection = K2Client.OpenConnection();
            try
            {
                //RedirectWorklistItem();
                string username = K2Helper.GetAuditUsername(WorkItem.Destination);
                connection.ImpersonateUser(username);

                K2Client.ExecuteAction(connection, SerialNumber, ActionInfo.ActionName, AuditDestination, _dataFields);

                connection.RevertUser();
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// 记录审批动作
        /// Reject需要
        /// 
        /// </summary>
        protected virtual void BuildActionRecord()
        {
            _actionRecordID = K2DataHelper.InsertActionRecord(
                WorkItem.ActivityName, ActionInfo.ActionType, ActionInfo.ActionName, ActionInfo.AuditRule,
                AppForm.CurrentUser.UserId, AppForm.CurrentUser.UserEn, AppForm.CurrentUser.UserCn,
                AppForm.DelegateFrom.UserId, AppForm.DelegateFrom.UserEn, AppForm.DelegateFrom.UserCn,
                ProcInstID, K2ActionRecordStatus.Normal, StoredDataFields, StoredXmlFields);
        }

        protected virtual void BuildMyActivity()
        {
            //获取这个Folio下所有的WorklistItem，包含Action动作之后产生的WorklistItem，也有可能包含已有的WorklistItem
            _allWorklistItemsAfterAction = K2Admin.GetWorklistItems(ProcessName, AppForm.Folio);
            //获取所有ProcInstID的MyActivity
            //用于Merge的MyActivity必须满足状态为0:正常 以及 1:挂起(有可能出现AskForMore或ForwordBack后,所以挂起状态的待办也可能用于Merge)
            _myActivitySetBeforeMoving = K2DataHelper.GetMyActivities(ProcInstID, new int[] { (int)MyActivityStatus.Normal, (int)MyActivityStatus.Suspended });
            MergeWorklistItemsAndMyActivitySet();
            MoveMyActivityList();
            CreateNewMyActivityList();
        }

        protected virtual void BuildProcessLog()
        {
            Flow_ProcessLogTableEntity processLog = new Flow_ProcessLogTableEntity();
            processLog.ProcessLogID = Guid.NewGuid();
            processLog.ProcName = WorkItem.ProcName;
            processLog.ActivityName = WorkItem.ActivityName;
            processLog.ActionType = ActionInfo.ActionType;
            processLog.OwnerID = AppForm.CurrentUser.UserId;
            processLog.OwnerEn = AppForm.CurrentUser.UserEn;
            processLog.OwnerCn = AppForm.CurrentUser.UserCn;
            processLog.DelegateFromID = AppForm.DelegateFrom.UserId;
            processLog.DelegateFromEn = AppForm.DelegateFrom.UserEn;
            processLog.DelegateFromCn = AppForm.DelegateFrom.UserCn;
            processLog.ApplicationNo = AppForm.ApplicationNo;
            processLog.ProcInstID = WorkItem.ProcInstId;
            processLog.NextAuditors = GetNextAuditorsConnect();
            processLog.CreatedDate = DateTime.Now;
            processLog.ActionRecordID = ActionRecordID;
            processLog.Status = 0;
            K2DataHelper.InsertProcessLog(processLog);
        }

        protected virtual void BuildProcessStamp()
        {
            //当配置中允许显示图章并且当前操作类型为Approve;Reject时新增图章
            if ((bool)GetCurrentProcessConfig().ShowStamp)
            {
                Flow_ProcessStampTableEntity processStamp = new Flow_ProcessStampTableEntity();
                processStamp.ProcessStampID = Guid.NewGuid();
                processStamp.ProcName = WorkItem.ProcName;
                processStamp.ActivityName = WorkItem.ActivityName;
                processStamp.ActionType = ActionInfo.ActionType;
                processStamp.OwnerID = AppForm.CurrentUser.UserId;
                processStamp.OwnerEn = AppForm.CurrentUser.UserEn;
                processStamp.OwnerCn = AppForm.CurrentUser.UserCn;
                processStamp.DelegateFromID = AppForm.DelegateFrom.UserId;
                processStamp.DelegateFromEn = AppForm.DelegateFrom.UserEn;
                processStamp.DelegateFromCn = AppForm.DelegateFrom.UserCn;
                processStamp.ApplicationNo = AppForm.ApplicationNo;
                processStamp.ProcInstID = WorkItem.ProcInstId;
                processStamp.ActionRecordID = ActionRecordID;
                processStamp.Status = 0;
                processStamp.CreatedDate = DateTime.Now;
                K2DataHelper.InsertProcessStamp(processStamp);
            }

            //当当前操作类型为撤回时清除所有图章
            //Todo:在重载中写

            //当当前操作为Drawback时并且后续待办事项未进行处理可以清除自己的图章
            //Todo:在重载中写
        }

        protected virtual void BuildLeadingTime()
        {
            Flow_LeadingTimeTableEntity leadingTime = new Flow_LeadingTimeTableEntity();
            leadingTime.LeadingTimeID = Guid.NewGuid();
            leadingTime.ProcName = WorkItem.ProcName;
            leadingTime.ActivityName = WorkItem.ActivityName;
            leadingTime.OwnerID = AppForm.CurrentUser.UserId;
            leadingTime.OwnerEn = AppForm.CurrentUser.UserEn;
            leadingTime.OwnerCn = AppForm.CurrentUser.UserCn;
            leadingTime.StartDate = WorkItem.ArrvialDate;
            leadingTime.EndDate = DateTime.Now;
            leadingTime.ApplicationNo = AppForm.ApplicationNo;
            leadingTime.ProcInstID = WorkItem.ProcInstId;
            leadingTime.ActionType = ActionInfo.ActionType;
            leadingTime.ActionRecordID = ActionRecordID;
            leadingTime.Status = 0;
            K2DataHelper.InsertLeadingTime(leadingTime);
        }

        protected virtual void BuildProcessComment()
        {
            if (!string.IsNullOrEmpty(Comment))
            {
                Flow_ProcessCommentTableEntity processComment = new Flow_ProcessCommentTableEntity();
                processComment.ProcessCommentID = Guid.NewGuid();
                processComment.ProcName = WorkItem.ProcName;
                processComment.ActivityName = WorkItem.ActivityName;
                processComment.OwnerID = AppForm.CurrentUser.UserId;
                processComment.OwnerEn = AppForm.CurrentUser.UserEn;
                processComment.OwnerCn = AppForm.CurrentUser.UserCn;
                processComment.DelegateFromID = AppForm.DelegateFrom.UserId;
                processComment.DelegateFromEn = AppForm.DelegateFrom.UserEn;
                processComment.DelegateFromCn = AppForm.DelegateFrom.UserCn;
                processComment.ApplicationNo = AppForm.ApplicationNo;
                processComment.ProcInstID = WorkItem.ProcInstId;
                processComment.ActionRecordID = ActionRecordID;
                processComment.Comment = Comment;
                processComment.CreatedDate = DateTime.Now;
                processComment.Status = 0;
                K2DataHelper.InsertProcessComment(processComment);
            }

            //当当前操作类型为撤回时清除所有Comment
            //Todo:在重载中写

            //当当前操作为Drawback时并且后续待办事项未进行处理可以清除自己的Comment
            //Todo:在重载中写
        }

        protected virtual void SendMail()
        {
        }
        private void SendMail2()
        {
            string actionType = GetActionType();

            Flow_ProcessMailConfigTableEntity mailConfig = K2DataHelper.GetProcessMailConfig(WorkItem.ProcName, WorkItem.ActivityName, actionType);
            if (mailConfig == null) return;

            if (!string.IsNullOrEmpty(mailConfig.EmailActionHandler) && !string.IsNullOrEmpty(mailConfig.TemplateKey))
            {
                Type type = Type.GetType(mailConfig.EmailActionHandler);
                IEmailAction ea = Activator.CreateInstance(type) as IEmailAction;
                ea.SendMail(AppForm.FormId, mailConfig.TemplateKey);
            }
        }

        protected virtual string GetActionType()
        {
            return string.Empty;
        }

        #endregion
    }
}
